package com.zhuss.food.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.food.order.DetailOrderBo;
import com.zhuss.bean.bo.food.order.SearchOrderBo;
import com.zhuss.bean.bo.food.order.wechat.ListOrderBo;
import com.zhuss.bean.unit.food.ProductItem;
import com.zhuss.bean.unit.food.TopicItem;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.food.food.DetailFoodVo;
import com.zhuss.bean.vo.food.order.ListOrderVo;
import com.zhuss.bean.vo.food.order.wechat.OrderProductVo;
import com.zhuss.bean.vo.food.order.wechat.OrderVo;
import com.zhuss.bean.vo.food.order.wechat.TopicBean;
import com.zhuss.bean.vo.food.product.DetailProductVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.core.db.entity.Company;
import com.zhuss.core.processor.CompanyRead;
import com.zhuss.enums.CompanyType;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.OrderDao;
import com.zhuss.food.db.dao.ext.OrderProductDao;
import com.zhuss.food.db.dao.ext.TopicDao;
import com.zhuss.food.db.entity.Order;
import com.zhuss.food.db.entity.OrderProduct;
import com.zhuss.util.DateUtil;


/**
 * 查询订单 processor
 * @Author: zhushanshuo
 * @Date: 2020/03/08 16:01
 */
@Component("orderRead")
public class OrderRead {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderProductDao orderProductDao;
	
	@Autowired
	private TopicDao topicDao;
	
	@Autowired
	private CompanyRead companyRead;
	
	@Autowired
	private ProductRead productRead;
	
	@Autowired
	private FoodRead foodRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	public Integer alreadyBuyNum(Long userId, Long productId) {
		List<Integer> statusList = new ArrayList<Integer>();
		statusList.add(OrderStatus.未接单.key());
		statusList.add(OrderStatus.未取餐.key());
		statusList.add(OrderStatus.已取餐.key());
		statusList.add(OrderStatus.已评价.key());
		Date now = new Date();
    	Date startDate = DateUtil.toDateOnly(now);
    	Date endDate = DateUtil.toDateOnlyEnd(now);
		List<Order> orderList = orderProductDao.selectOrderList(userId, productId, statusList, startDate, endDate);
		return orderList.size();
	}
	
	/**
	 * 订单列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchOrderBo bo) {
		PageVo<ListOrderVo> result = new PageVo<>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        if (bo.getCreateTime() != null) {
        	bo.setStartDate(DateUtil.toDateOnly(bo.getCreateTime()));
        	bo.setEndDate(DateUtil.toDateOnlyEnd(bo.getCreateTime()));
        }
        result.setItems(transferToVo(orderDao.list(bo)));
        result.setTotal(orderDao.countList(bo));
		return result;
	}
	
	private List<ListOrderVo> transferToVo(List<ListOrderVo> orderList) {
		for (ListOrderVo item : orderList) {
			Long orderId = item.getOrderId();
			List<OrderProduct> opList = orderProductDao.listOrderProduct(orderId);
			List<ProductItem> productList = new ArrayList<>();
			for (OrderProduct op : opList) {
				ProductItem pi = new ProductItem();
				pi.setCount(op.getCount());
				pi.setProductId(op.getProductId());
				pi.setProductName(op.getProductName());
				productList.add(pi);
			}
			item.setProductList(productList);
			
			List<TopicItem> topicList = topicDao.listByOrder(orderId);
			
			item.setTopicList(topicList);
			
		}
		
		return orderList;
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long orderId) throws WpsException {
		Object result = orderDao.detail(orderId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	
	public OrderVo detailOrder(DetailOrderBo bo) throws WpsException {
		Long orderId = bo.getOrderId();
		Integer area = bo.getConcurrentArea();
		String serviceCompanyphone = initServiceCompanyphone(area);
		Order o = orderDao.selectByPrimaryKey(orderId);
		OrderVo order = new OrderVo();
		initOrderVo(order, o, serviceCompanyphone);
		List<TopicItem> list = topicDao.listByOrder(orderId);
		List<TopicBean> topicList = new ArrayList<>();
		for (TopicItem item : list) {
			TopicBean tb = new TopicBean();
			tb.setContent(item.getContent());
			tb.setLevel(item.getLevel());
			tb.setOrderNo(item.getOrderNo());
			tb.setTopicId(item.getTopicId());
			tb.setTopicTime(item.getCreateTime().getTime());
			topicList.add(tb);
		}
		order.setTopicList(topicList);
		return order;
	}
	
	public List<OrderVo> listOrder(ListOrderBo bo) throws WpsException {
		
		List<OrderVo> result = new ArrayList<OrderVo>();
		Long creatorId = bo.getConcurrentUserId();
		Integer area = bo.getConcurrentArea();
		
		SearchOrderBo searchBo = new SearchOrderBo();
		BeanUtils.copyProperties(bo, searchBo);
		searchBo.setArea(area);
		searchBo.setCreatorId(creatorId);
		
		String serviceCompanyphone = initServiceCompanyphone(area);
		
		List<Order> orderList = orderDao.listOrder(searchBo);
		for (Order o : orderList) {
			OrderVo order = new OrderVo();
			initOrderVo(order, o, serviceCompanyphone);
			result.add(order);
		}
		return result;
	}
	
	private String initServiceCompanyphone(Integer area) {
		List<Company> companyList = companyRead.listByType(CompanyType.餐饮公司.key(), area);
		String serviceCompanyphone = "";
		if (companyList != null && !companyList.isEmpty()) {
			Company company = companyList.get(0);
			serviceCompanyphone = company.getLinkPhone();
			
		}
		return serviceCompanyphone;
	}
	
	private void initOrderVo(OrderVo order, Order o, String serviceCompanyphone) throws WpsException {
		order.setAmount(o.getAmount());
		order.setCount(o.getCount());
		order.setOrderId(o.getOrderId());
		order.setOrderNo(o.getOrderNo());
		order.setProductCount(o.getProductCount());
		order.setStatus(o.getStatus());
		order.setType(o.getType());
		order.setCreateTime(o.getCreateTime().getTime());
		order.setMaxFetchFoodTime(o.getCreateTime().getTime());
		order.setDiningType(o.getDiningType());
		List<OrderProductVo> opvList = new ArrayList<OrderProductVo>();
		List<OrderProduct> orderProductList = orderProductDao.listOrderProduct(o.getOrderId());
		for (OrderProduct op : orderProductList) {
			OrderProductVo opv = new OrderProductVo();
			opv.setAmount(op.getAmount());
			opv.setCount(op.getCount());
			opv.setOrderNo(op.getOrderNo());
			opv.setOrderProductId(op.getOrderProductId());
			opv.setPrice(op.getPrice());
			opv.setProductName(op.getProductName());
			opv.setProductNo(op.getProductNo());
			Long productId = op.getProductId();
			Long foodId = ((DetailProductVo) productRead.detail(productId)).getFoodId();
			DetailFoodVo food = ((DetailFoodVo) foodRead.detail(foodId));
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(food.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			opv.setPicUrl(downloadVo.getUrl());
			opvList.add(opv);
		}
		order.setOrderProductList(opvList);
		order.setServiceCompanyphone(serviceCompanyphone);
		Date expectFetchTimeStart = o.getExpectFetchTimeStart();
		Date expectFetchTimeEnd = o.getExpectFetchTimeEnd();
		order.setExpectFetchTimeStart(expectFetchTimeStart == null ? null : expectFetchTimeStart.getTime());
		order.setExpectFetchTimeEnd(expectFetchTimeEnd == null ? null : expectFetchTimeEnd.getTime());
		order.setDate(o.getDate() != null ? o.getDate().getTime() : null);
		order.setFoodTime(o.getFoodTime());
		order.setPersonNum(o.getPersonNum());
		order.setHavingStandard(o.getHavingStandard());
		order.setDrinkType(o.getDrinkType());
		order.setArea(o.getArea());
		
	}
	
	

}
